package com.baiwei.vpnapi.utils;

import org.springframework.stereotype.Component;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: vpnapi
 * @description: 字段验证
 * @author: Mr. Stone
 * @create: 2020-02-26 11:38
 **/
@Component
public class FieldChecker {
    /**
    * @Description: 验证电话
    * @Param:
    * @return:
    * @Author: Mr.Wang
    * @Date: 2020/2/26
    */
    public static boolean isValidPhone(String phone){
        for (int i = 0; i < phone.length(); i++) {
            System.out.println(phone.charAt(i));
            if (!Character.isDigit(phone.charAt(i))) {
                return false;
            }
        }
        return true;
    }
    /**
    * @Description: 验证用户名
    * @Param:
    * @return:
    * @Author: Mr.Wang
    * @Date: 2020/2/26
    */
    public static boolean isValidUsername(String username) {
        String pattern = "[a-zA-Z0-9]*";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(username);
        return m.matches();
    }

    /**
    * @Description: 地区编码效验
    * @Param:
    * @return:
    * @Author: Mr.Wang
    * @Date: 2020/2/26
    */
    public static boolean isValidCountryCode(String countryCode) {
        String [] all = {"1", "7", "20", "27", "30", "31", "32", "33", "34", "36", "39", "40", "41", "43", "44", "45", "46", "47", "48", "49", "51", "52", "53", "54", "55", "56", "57", "58", "60", "61", "62", "63", "64", "65", "66", "81", "82", "84", "86", "90", "91", "92", "93", "94", "95", "98", "212", "213", "216", "218", "220", "221", "222", "223", "224", "225", "226", "227", "229", "230", "231", "232", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "243", "244", "245", "248", "249", "250", "251", "252", "253", "254", "255", "256", "257", "258", "260", "261", "262", "263", "264", "265", "266", "267", "268", "269", "290", "291", "297", "298", "299", "350", "351", "352", "353", "354", "355", "356", "357", "358", "359", "370", "371", "372", "373", "374", "375", "376", "377", "378", "380", "385", "386", "387", "389", "418", "420", "421", "423", "500", "501", "502", "503", "504", "505", "506", "507", "508", "509", "590", "591", "592", "593", "594", "595", "596", "597", "598", "599", "670", "672", "673", "674", "675", "676", "677", "678", "679", "680", "681", "682", "683", "685", "686", "687", "688", "689", "690", "691", "692", "850", "852", "853", "855", "856", "880", "886", "960", "961", "962", "963", "964", "965", "966", "967", "968", "970", "971", "972", "973", "974", "975", "976", "977", "992", "993", "994", "995", "996", "998", "1242", "1246", "1264", "1268", "1284", "1340", "1345", "1441", "1473", "1649", "1664", "1670", "1671", "1684", "1758", "1767", "1784", "1787", "1809", "1829", "1868", "1869", "1876", "1939"};
        for (String str: all) {
            if(str.equals(countryCode)) {
                return true;
            }
        }
        return false;
    }
    /**
    * @Description: 验证邮箱有效性
    * @Param:
    * @return:
    * @Author: Mr.Wang
    * @Date: 2020/2/26
    */
    public static boolean isValidEmail(String email){
        String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        boolean isMatched = matcher.matches();
        return isMatched;
    }
    /**
    * @Description: 校验密码有效性
    * @Param:
    * @return:
    * @Author: Mr.Wang
    * @Date: 2020/2/26
    */
    public static boolean isValidPassword(String password) {
        if(password == null || password.length() < 8) {
            return false;
        }
        String check = "^[a-zA-Z0-9]*$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(password);
        boolean isMatched = matcher.matches();
        return  isMatched;
    }
}
